package com.zqweb.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    // RabbitMQ的配置类
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 自定义消息的序列化机制（发送与存储）
    @Bean
    public MessageConverter customMsgSerializer(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange buildStockEventExchange(){
        return new TopicExchange("stock-event-exchange",true,false);
    }
    @Bean
    public Queue buildStockLockQueue(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange","stock-event-exchange");
        args.put("x-dead-letter-routing-key","stock.release");
        args.put("x-message-ttl",60000 * 2);// 2min
        return new Queue("stock.delay.queue",true,false,false,args);
    }
    @Bean
    public Queue buildStockReleaseQueue(){
        return new Queue("stock.release.stock.queue",true,false,false);
    }
    @Bean
    public Binding buildBindingToStockReleaseQueue(){
        return new Binding("stock.release.stock.queue",
          Binding.DestinationType.QUEUE,"stock-event-exchange","stock.release.#",null);
    }
    @Bean
    public Binding buildBindingToStockLockQueue(){
        return new Binding("stock.delay.queue",
          Binding.DestinationType.QUEUE,"stock-event-exchange","stock.locked",null);
    }


}
